package org.example.service;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.client.spring.annotation.ExternalTaskSubscription;
import org.camunda.bpm.client.task.ExternalTaskHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 */
@Configuration
@Slf4j
public class RepairSubscribe {

    //全局配置优先级小余私有配置，因此超时会以此处为准
    @Bean
    @ExternalTaskSubscription(topicName = "try_self_repair", processDefinitionKeyIn = {"Process_test_service_task_v2", "Process_execution_listener"}, lockDuration = 2000)
    public ExternalTaskHandler TrySelfRepair() {
        return ((externalTask, externalTaskService) -> {
            log.info("外部任务进入尝试自修");
            /**
             * 是否免费修
             * */
            Boolean isFree = true;
            if (isFree) {
                log.info("免费维修");
                externalTaskService.complete(externalTask);
            } else {
                log.info("自己修");
                /**
                 *  message stacktrace 重试次数 重试超时时间
                 * 重试次数为0时，会创建一个异常事件
                 * */
                externalTaskService.handleFailure(externalTask, "message-自己修不好", "stacktrace", 0, 5000);
            }
            log.info("外部任务结束尝试自修");
        });
    }
}
